package com.andy.infrastructure.util;

import freemarker.template.Template;
import freemarker.template.TemplateException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class FreeMarkerUtil {

    @Value("${myClasspath}")
    private String myClasspath;

    @Autowired
    private FreeMarkerUtil freeMarkerUtil;

    public String getFreeMarkerString(freemarker.template.Configuration cfg,String ftlName,String input){
        String result = "";
        try {

            Template t = cfg.getTemplate(ftlName);
            Map<String, Object> map =  freeMarkerUtil.putInput(input);

            // 输出到控制台
            StringWriter writer = new StringWriter();
            t.process(map, writer);
            result = writer.toString();

        } catch (IOException e) {
            e.printStackTrace();
        } catch (TemplateException e) {
            e.printStackTrace();
        }

        return result;
    }

    public String getFreeMarkerString(freemarker.template.Configuration cfg,String ftlName,Map<String, Object> map){
        String result = "";
        try {
            Template t = cfg.getTemplate(ftlName);

            // 输出到控制台
            StringWriter writer = new StringWriter();
            t.process(map, writer);
            result = writer.toString();

        } catch (IOException e) {
            e.printStackTrace();
        } catch (TemplateException e) {
            e.printStackTrace();
        }

        return result;
    }

    /**
     * 放入带换行的input文本
     * @param input
     */
    public Map<String,Object> putInput(String input){
        //1-拆成一行一行的
        String[] inputLineArr = input.split("\n");

        //2-装载到kv
        Map<String,String> inputMap = new HashMap<String,String>();
        for(String inputLine:inputLineArr){
            String key = inputLine.substring(0, inputLine.indexOf(":"));
            String val = inputLine.substring(inputLine.indexOf(":")+1);
            inputMap.put(key,val);
        }

        //3-转化形态，放入最终的map
        Map<String,Object> resultMap = new HashMap<String,Object>();
        for(Map.Entry entry:inputMap.entrySet()){
            String key = (String)entry.getKey();
            String value = (String)entry.getValue();
            put(resultMap,key,value);
        }

        resultMap.put("classpath",myClasspath);

        return resultMap;
    }

    /**
     * put单个kv
     * @param map
     * @param key
     * @param val
     */
    public void put(Map<String, Object> map,String key,String val){
        if(StringUtils.isBlank(val)) {
            return;
        }

        if(val.contains(";")){

            //数组逻辑 val:[String,orderNo][Integer,id][String,couponCode]
            List<Map<String, Object>> inputList = new ArrayList <>();
            //转成数组 String,orderNo
            String[] lineArr = val.split(";");
            for(String line:lineArr){
                //转成单个字段 String
                String[] colArr = line.split(",");
                Map<String, Object> tempMap = new HashMap <>();
                for(int i=0;i<colArr.length;i++){
                    String tempVal = colArr[i];
                    Map <String, String> transfer = StringCaseUtil.transferStr(tempVal);
                    tempMap.put("t"+i,val);
                    tempMap.put("t"+i+"_I_I",transfer.get("I_I"));
                    tempMap.put("t"+i+"_Iii",transfer.get("Iii"));
                    tempMap.put("t"+i+"_iii",transfer.get("iii"));
                }
                inputList.add(tempMap);
            }
            map.put(key,inputList);

        }else{
            //字符串逻辑
            Map <String, String> transfer = StringCaseUtil.transferStr(val);
            map.put(key,val);
            map.put(key+"_I_I",transfer.get("I_I"));
            map.put(key+"_Iii",transfer.get("Iii"));
            map.put(key+"_iii",transfer.get("iii"));
        }
    }

    /**
     * 增强目前map中的kv
     * @param inputMap
     * @return
     */
    public Map<String, Object> advanceMapCase(Map<String, Object> inputMap){
        Map<String,Object> resultMap = new HashMap<String,Object>();
        for(Map.Entry entry:inputMap.entrySet()){
            String key = (String)entry.getKey();
            String value = (String)entry.getValue();
            put(resultMap,key,value);
        }

        return resultMap;
    }


}
